﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Syufy.BINLookup.Web.Models;
using Syufy.BINLookup.Web.Helpers;
using Syufy.BINLookup.Web.Interfaces;

namespace Syufy.BINLookup.Web.Controllers.Api
{
    /// <summary>
    /// Web API for Membership Price.
    /// </summary>
    public class MembershipPriceController : ApiController
    {
        private IUnitOfWork _unitOfWork;

        /// <summary>
        /// Membership price controller constructor.
        /// </summary>
        public MembershipPriceController() : this(new UnitOfWork<BinEntities>())
        {
        }

        /// <summary>
        /// Membership price controller constructor (dependency injection).
        /// </summary>
        /// <param name="unitOfWork"></param>
        public MembershipPriceController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        // GET: api/MembershipPrice
        /// <summary>
        /// Get all Membership Price data.
        /// </summary>
        /// <returns>List of Membership Price data.</returns>
        [ResponseType(typeof(IEnumerable<tblMembershipPrice>))]
        public IHttpActionResult Get()
        {
            return Ok(_unitOfWork.GetRepository<tblMembershipPrice>().Get());
        }

        // GET: api/MembershipPrice/5
        /// <summary>
        /// Get specific Membership Price data.
        /// </summary>
        /// <param name="id">Site ID.</param>
        /// <returns>Membership price based on specified Site ID.</returns>
        [ResponseType(typeof(tblMembershipPrice))]
        public IHttpActionResult Get(int id)
        {
            tblMembershipPrice tblMembershipPrice = _unitOfWork.GetRepository<tblMembershipPrice>().GetByID(id);
            if (tblMembershipPrice == null)
            {
                return NotFound();
            }

            return Ok(tblMembershipPrice);
        }

        // POST: api/MembershipPrice
        /// <summary>
        /// Create Membership Price record.
        /// </summary>
        /// <param name="price">Membership price object.</param>
        /// <returns>URL for Membership Price.</returns>
        [ResponseType(typeof(tblMembershipPrice))]
        public async Task<IHttpActionResult> Post(tblMembershipPrice price)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _unitOfWork.GetRepository<tblMembershipPrice>().Insert(price);
            await _unitOfWork.SaveAsync();

            return CreatedAtRoute("DefaultApi", new { id = price.siteId }, price);
        }

        // PUT: api/MembershipPrice/5
        /// <summary>
        /// Update Membership Price data based on specific site.
        /// </summary>
        /// <param name="id">Site ID.</param>
        /// <param name="price">Membership price object.</param>
        /// <returns></returns>
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> Put(int id, tblMembershipPrice price)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            tblMembershipPrice update = _unitOfWork.GetRepository<tblMembershipPrice>().GetByID(id);
            if (update == null)
            {
                return NotFound();
            }

            update.addAdultDues = price.addAdultDues.ReturnDefaultIfNullableDecimal(update.addAdultDues);
            update.addAdultEnrollment = price.addAdultEnrollment.ReturnDefaultIfNullableDecimal(update.addAdultEnrollment);
            update.childDues = price.childDues.ReturnDefaultIfNullableDecimal(update.childDues);
            update.childEnrollment = price.childEnrollment.ReturnDefaultIfNullableDecimal(update.childEnrollment);
            update.mainDues = price.mainDues.ReturnDefaultIfNullableDecimal(update.mainDues);
            update.mainEnrollment = price.mainEnrollment.ReturnDefaultIfNullableDecimal(update.mainEnrollment);
            update.taxRate = price.taxRate.ReturnDefaultIfNullableDecimal(update.taxRate);
            update.mTypeId = price.mTypeId.ReturnDefaultIfDefaultInt(update.mTypeId);
            update.siteId = price.siteId.ReturnDefaultIfDefaultInt(update.siteId);
            update.siteName = price.siteName.ReturnDefaultIfNull(update.siteName);
            update.surchargeRate = price.surchargeRate.ReturnDefaultIfNullableDecimal(update.surchargeRate);
            update.discountRate = price.discountRate.ReturnDefaultIfNullableDecimal(update.discountRate);

            try
            {
                await _unitOfWork.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!tblMembershipPriceExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        /// <summary>
        /// IDisposable override.
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private bool tblMembershipPriceExists(int id)
        {
            return _unitOfWork.GetRepository<tblMembershipPrice>().GetByID(id) == null;
        }
    }
}